package edu.codink.xq.sys.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import edu.codink.common.base.BaseController;
import edu.codink.common.base.Page;
import edu.codink.xq.center.entity.Userinfo;

@Controller
public class UserController extends BaseController
{
	@Autowired
	private UserHbmService userHbmService;
	
	private final String USERS = "user";
	private final String ADMIN = "admin";
	private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 浏览用户
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("sys/user/list")
	public ModelAndView list(Userinfo user, HttpServletRequest request)
	{
		Page page = getPage(request);
		List<Userinfo> list = userHbmService.list(user, page);
		return buildView().setViewName("sys/user/list.jsp").addObject(PARAM_PAGE, page).addObject(USERS,user).addObject(PARAM_LIST, list).getView();
	}
	
	/**
	 * 跳转到新增页面
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("sys/user/add")
	public ModelAndView add(Userinfo user, HttpServletRequest request)
	{
		String id = request.getParameter("id"); 
		//修改页面
		if (id != null)
		{
			user = this.getUserInfoByid(id);
			return buildView().setViewName("sys/user/edit.jsp").addObject(USERS,user).getView();
		}
		//新增页面
		return buildView().setViewName("sys/user/add.jsp").addObject(USERS,user).getView();
	}
	
	/**
	 * 保存新增的用户
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("sys/user/save")
	public ModelAndView save(Userinfo user, HttpServletRequest request)
	{
		if (user != null)
		{
			if (user.getCreatetimeStr() != null && !user.getCreatetimeStr().equals("")) 
			{
				try
				{
					user.setCreatetime(format.parse(user.getCreatetimeStr()));
				} catch (ParseException e)
				{
					e.printStackTrace();
				}
			}
			userHbmService.save(user);
		}
		return buildView().setViewName("sys/user/add.jsp").addObject(USERS,user).getView();
	}

	@RequestMapping("sys/user/delete")
	public ModelAndView delete(Userinfo user,HttpServletRequest request)
	{
		String id = request.getParameter("id"); 
		String msg = "未知原因，删除失败，请联系管理人员";
		if (id != null && !"".equals(id))
		{
			Integer userid= Integer.parseInt(id);
			Userinfo cuser = getUserInfoByid(id);
			if (ADMIN.equals(cuser.getLoginname()))
			{
				msg = "该用户为系统内置账户，不可删除！";
			}
			else if (user.getYgbh() == cuser.getYgbh())
			{
				msg = "您不能删除自己账户！";
			}
			else
			{
				Integer count = userHbmService.deleteUser(userid);
				if(count > 1)
				{
					msg = "已经成功的从数据库中删除该用户！";
				}
			}
		}
		request.setAttribute("msg", msg);
		return buildView().setViewName("sys/user/faiOrSuccessl.jsp").addObject(USERS,user).getView();
	}
	
	/**
	 * 根据id查找用户
	 * @param userid
	 * @return
	 */
	private Userinfo getUserInfoByid(String userid)
	{
		return userHbmService.query(Integer.parseInt(userid));
	}
}
